/**
 * 显示弹窗方法
 * @param data 角色数据
 * @param that 操作对象
 * @param fn 回调函数
 */
function showPopup(data,that) {
    var $x = that.offset().left, $y = that.parent().offset().top;
    var $content = "<select id='roleName' name='roleName'>";
    if (data != null) {
        for (var i = 0; i < data.length; i++) {
            $content+="<option "+(data[i].roleId == that.attr("data")?'selected="selected"':"")+" value=\""+(data[i].roleId)+"\">"+data[i].roleName+"</option>";
        }
    }
    $content += "</select>";
    if($("#edit_popup").length==0) {
        addNode(that,$x,$y,"分配角色",$content);
    }else{
        if (that.parent().find("#edit_popup").length == 0) {
            $("#edit_popup").remove();
            addNode(that,$x,$y,"分配角色",$content);
        }else{
            $("#edit_popup").remove();
        }
    }
    $("#confirmBtn").click(function () {
        assignUserRole(that);
    });
}

function assignUserRole(element) {
    var $selected = $("#roleName").val();
    if (element.attr("data") == $selected){
        window.location.reload();
        return;
    }
    if ($selected && element.attr("index")){
        $.ajax({
            type: "POST",
            url: "assignUserRole",
            data: {"roleId":$selected,"userId":element.attr("index")},
            timeout: 3000,
            dataType: "text",
            success: function (res) {
                if (res != "success") {
                    $(".popup_con .popup_input").remove();
                    $(".popup_con .popup_input").before("<div style='line-height: 15px'><span class='msg'>操作失败！</span></div>");
                }else {
                    window.location.reload();
                }
            },
            error: function() {
                $(".popup_con .popup_input").remove();
                $(".popup_con .popup_input").before("<div style='line-height: 15px'><span class='msg'>操作出现错误！</span></div>");
            }
        });
    }
}

